﻿using Biomedica.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace NuiDicomPlayer.Controls
{
    public class NuiControl : UserControl
    {
        #region Routed Event
        public static readonly RoutedEvent HandEnteredRoutedEvent = EventManager.RegisterRoutedEvent("OnHandEnteredEvent", RoutingStrategy.Bubble, typeof(EventHandler), typeof(NuiControl));
        public static readonly RoutedEvent HandInEventRoutedEvent = EventManager.RegisterRoutedEvent("OnHandInEvent", RoutingStrategy.Bubble, typeof(EventHandler), typeof(NuiControl));
        public static readonly RoutedEvent HandExitedEventRoutedEvent = EventManager.RegisterRoutedEvent("OnHandExitedEvent", RoutingStrategy.Bubble, typeof(EventHandler), typeof(NuiControl));
        public static readonly RoutedEvent OnHandFiredEventRoutedEvent = EventManager.RegisterRoutedEvent("OnHandFiredEvent", RoutingStrategy.Bubble, typeof(EventHandler), typeof(NuiControl));
        
        public event EventHandler OnHandEnteredEvent { add { AddHandler(HandEnteredRoutedEvent, value); } remove { RemoveHandler(HandEnteredRoutedEvent, value); } }
        public event EventHandler OnHandInEvent { add { AddHandler(HandInEventRoutedEvent, value); } remove { RemoveHandler(HandInEventRoutedEvent, value); } }
        public event EventHandler OnHandExitedEvent { add { AddHandler(HandExitedEventRoutedEvent, value); } remove { RemoveHandler(HandExitedEventRoutedEvent, value); } }
        public event EventHandler OnHandFiredEvent { add { AddHandler(OnHandFiredEventRoutedEvent, value); } remove { RemoveHandler(OnHandFiredEventRoutedEvent, value); } }
        #endregion

        #region DpendancyProparties

        public static readonly DependencyProperty IsMagnitsedDependancyObject = DependencyProperty.Register("IsMagnitsed", typeof(Boolean), typeof(NuiControl), new PropertyMetadata());
        public Boolean IsMagnitsed
        {
            get { return (Boolean)GetValue(IsMagnitsedDependancyObject); }
            set { SetValue(IsMagnitsedDependancyObject, value); }
        }


        public static readonly DependencyProperty OnEnterdColor = DependencyProperty.Register("EnterdBkg", typeof(Color), typeof(NuiControl), new PropertyMetadata());
        public Color EnterdBkg
        {
            get { return (Color)GetValue(OnEnterdColor); }
            set { SetValue(OnEnterdColor, value); }
        }


        public static readonly DependencyProperty OnExitedColor = DependencyProperty.Register("ExitedBkg", typeof(Color), typeof(NuiControl), new PropertyMetadata());
        public Color ExitedBkg
        {
            get { return (Color)GetValue(OnExitedColor); }
            set { SetValue(OnExitedColor, value); }
        }

        #endregion

         public Vector4 Bounds
        {
            set;
            get;
        }
         private int State;

         public NuiControl()
        {
            
        }

      
         public virtual void OnHandExit(EventArgs eventArgs)
        {
 	       RaiseEvent(new RoutedEventArgs(HandExitedEventRoutedEvent,this));
        }
         public virtual void OnHandIn(EventArgs eventArgs)
       {
 	     RaiseEvent(new RoutedEventArgs(HandInEventRoutedEvent,this));
        }
         public virtual void OnHandEnter(EventArgs eventArgs)
         {
 	       RaiseEvent(new RoutedEventArgs(HandEnteredRoutedEvent,this));
         }
         public virtual void OnHandFired(EventArgs eventArgs)
         {
             RaiseEvent(new RoutedEventArgs(OnHandFiredEventRoutedEvent, this));
         }
         public void GetBounds()
         {
            Biomedica.Graphics.Vector4 v4 = Biomedica.Graphics.Vector4.Zero;
            v4.Left = (float)this.TransformToAncestor(Application.Current.MainWindow).Transform(new Point(0, 0)).X;
            v4.Top = (float)this.TransformToAncestor(Application.Current.MainWindow).Transform(new Point(0, 0)).Y;
            v4.Right = (float)this.ActualWidth + v4.Left;
            v4.Buttom = (float)this.ActualHeight + v4.Top;
            this.Bounds = v4;
        }
         public void CheckIfMagnitised(Point Location)
         {
             double dist=Bounds.Distance(new Vector2((float)Location.X,(float)Location.Y));
 
         }
         public void HandHitTest(Point Location)
         {

             this.GetBounds();
             if (App.ActiveMouse.Enabled)
             {
                 #region Action
                 switch (State)
                 {
                     case (0):
                        
                         break;
                     case (1):
                         App.ActiveMouse.OwnerControl = this;

                         OnHandEnter(new EventArgs());
                         break;
                     case (2):
                         
                         
                         OnHandIn(new EventArgs());
                         break;

                     case (3):
                         App.ActiveMouse.OwnerControl = null;
                         OnHandExit(new EventArgs());
                         break;
                 }
                 #endregion
                 #region Transtions
                 switch (State)
                 {
                     case (0):// idle
                         if (Bounds.Vector2InBound(new Vector2((float)Location.X, (float)Location.Y))) State = 1;// transate to enter
                         else State = 0;
                         break;
                     case (1):// OnEnter
                         if (Bounds.Vector2InBound(new Vector2((float)Location.X, (float)Location.Y))) State = 2;
                         else State = 3;
                         break;
                     case (2)://OnInside
                         if (Bounds.Vector2InBound(new Vector2((float)Location.X, (float)Location.Y))) State = 2;
                         else State = 3;
                         break;
                     case (3)://OnExit
                         if (Bounds.Vector2InBound(new Vector2((float)Location.X, (float)Location.Y))) State = 1;
                         else State = 0;
                         break;
                 }
                 #endregion
             }
         }
    }
}
